package br.com.edumobi.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

import br.com.edumobi.R;
import br.com.edumobi.adapters.ListaQuestoesAdapter;
import br.com.edumobi.dados.modelos.Questao;

public class MEQuestoesFragment extends GenericoFragment {

    private ListView listViewMEQuestoesFragment;

    private AcoesMEQuestoesFragment activity;

    private AdapterView.OnItemClickListener clickListViewMEQuestoesFragment = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            activity.abrirQuestao(position);
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesMEQuestoesFragment) getActivity();

        carregarListViewQuestoes();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me_questoes, container, false);

        listViewMEQuestoesFragment = (ListView) view.findViewById(R.id.listViewMEQuestoesFragment);

        listViewMEQuestoesFragment.setOnItemClickListener(clickListViewMEQuestoesFragment);

        return view;
    }

    @Override
    public void exibirComponentes() {
        listViewMEQuestoesFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        listViewMEQuestoesFragment.setVisibility(View.GONE);
    }

    private void carregarListViewQuestoes() {
        List<Questao> questoes = activity.obterQuestoes();
        listViewMEQuestoesFragment.setAdapter(new ListaQuestoesAdapter(questoes, true));
    }

    public interface AcoesMEQuestoesFragment extends AcoesGenericoFragment {
        List<Questao> obterQuestoes();
        void abrirQuestao(int indexQuestao);
    }
}
